﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using SEO.Model;
using System.Data.SQLite;

namespace SEO.DAL
{
    /// <summary>
    /// 版权所有: 版权所有(C) 2011，Adin Lee
    /// 内容摘要: 推广信息采集表
    /// 完成日期：2011年6月14日
    /// 版    本：V1.0 
    /// 作    者：Adin
    /// </summary>
    public class InformationDAL
    {

        /// <summary>
        /// 获取Information表中数据
        /// <returns>返回List</returns>
        /// </summary>
        public List<Information> GetAllInformationDAL()
        {
            List<Information> list = new List<Information>();
            string strSql = "select * from Information";
            try
            {
                SQLiteConnection cn = new SQLiteConnection(SEO.DBSqlHelp.CSDBSQLiteHelper.ConnectionSQLite());
                cn.SetPassword(SEO.Utility.CommonHelper.SQLITEPASSWORD);
                cn.Open();
                SQLiteCommand cm = new SQLiteCommand(strSql, cn);
                SQLiteDataReader dr = cm.ExecuteReader();
                while (dr.Read())
                {
                    Information imn = new Information();
                    imn.SEID = int.Parse(dr["se_id"].ToString());
                    imn.KeyName = dr["key_name"].ToString();
                    imn.SEType = dr["se_type"].ToString();
                    imn.InfoTitle = dr["info_title"].ToString();
                    imn.InfoContent = dr["info_content"].ToString();
                    imn.Position = dr["position"].ToString();
                    imn.WebUrl = dr["web_url"].ToString();
                    imn.PhoneResult = dr["phone_result"].ToString();
                    imn.CompanyName = dr["company_name"].ToString();
                    imn.CompanyPhone = dr["company_phone"].ToString();
                    imn.CompanyEmail = dr["company_email"].ToString();
                    imn.ContactUs = dr["contact_us"].ToString();
                    list.Add(imn);
                }
                dr.Dispose();
                cn.Close();
            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("绑定Information -> " + ex.Message);
            }
            return list;
        }

        /// <summary>
        /// 根据指定SQL语句获取Information表中数据
        /// <param name="strSql">SQL语句</param>
        /// </summary>
        /// <returns>返回List</returns>
        public List<Information> GetInformationByWhereDAL(string strSql)
        {
            List<Information> list = new List<Information>();
            try
            {
                SQLiteConnection cn = new SQLiteConnection(SEO.DBSqlHelp.CSDBSQLiteHelper.ConnectionSQLite());
                cn.SetPassword(SEO.Utility.CommonHelper.SQLITEPASSWORD);
                cn.Open();
                SQLiteCommand cm = new SQLiteCommand(strSql, cn);
                SQLiteDataReader dr = cm.ExecuteReader();
                while (dr.Read())
                {
                    Information imn = new Information();
                    imn.SEID = int.Parse(dr["se_id"].ToString());
                    imn.KeyName = dr["key_name"].ToString();
                    imn.SEType = dr["se_type"].ToString();
                    imn.InfoTitle = dr["info_title"].ToString();
                    imn.InfoContent = dr["info_content"].ToString();
                    imn.Position = dr["position"].ToString();
                    imn.WebUrl = dr["web_url"].ToString();
                    imn.PhoneResult = dr["phone_result"].ToString();
                    imn.CompanyName = dr["company_name"].ToString();
                    imn.CompanyPhone = dr["company_phone"].ToString();
                    imn.CompanyEmail = dr["company_email"].ToString();
                    imn.ContactUs = dr["contact_us"].ToString();
                    list.Add(imn);
                }
                dr.Dispose();
                cn.Close();
            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("根据指定SQL语句获取Information表中数据 -> " + ex.Message);
            }
            return list;
        }

        /// <summary>
        /// 往Information表中插入数据
        /// <param name="ks">实例化Information类</param>
        /// </summary>
        /// <returns>返回影响行数</returns>
        public int InsertInformationDAL(SEO.Model.Information imn)
        {
            int iNum = 0;
            string strSql = "insert into Information(se_id,key_name,se_type,info_title,info_content,position,web_url,phone_result,company_name,company_phone,company_email,contact_us) " +
             " values(" + imn.SEID + ",'" + imn.KeyName + "','" + imn.SEType + "','" + imn.InfoTitle + "','" + imn.InfoContent + "','" + imn.Position + "','" + imn.WebUrl + "','" + imn.PhoneResult + "','" + imn.CompanyName + "','" + imn.CompanyPhone + "','" + imn.CompanyEmail + "','" + imn.ContactUs + "')";
            try
            {
                iNum = SEO.DBSqlHelp.CSDBSQLiteHelper.ExecuteSql(strSql);
            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("往Information表中插入数据 -> " + ex.Message);
            }
            return iNum;
        }

        /// <summary>
        /// 根据指定条件删除Information表中数据
        /// <param name="strSql">SQL语句</param>
        /// </summary>
        /// <returns>返回影响行数</returns>
        public int DeleteInformationDAL(string strSql)
        {
            int iNum = 0;
            try
            {
                iNum = SEO.DBSqlHelp.CSDBSQLiteHelper.ExecuteSql(strSql);
            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("根据指定条件删除Information表中数据 -> " + ex.Message);
            }
            return iNum;
        }

        /// <summary>
        /// 更新Information表中数据is_post
        /// </summary>
        /// <param name="ks">实例化Information类</param>
        /// <returns>返回影响行数</returns>
        public int UpdateInformationDAL(SEO.Model.Information imn)
        {
            int iNum = 0;
            string strSql = "update Information set se_type=1 where se_id=" + imn.SEID;
            try
            {
                iNum = SEO.DBSqlHelp.CSDBSQLiteHelper.ExecuteSql(strSql);
            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("更新Information表中数据 -> " + ex.Message);
            }
            return iNum;
        }

        /// <summary>
        /// 绑定表Information到DataGridView
        /// <param name="dgv">DataGridView控件名称</param>
        /// <param name="strSql">SQL语句</param>
        /// </summary>
        public void BindInformationDAL(System.Windows.Forms.DataGridView dgv, string strSql)
        {
            try
            {
                //清除最后自动生成的行
                dgv.AutoGenerateColumns = false;
                SQLiteConnection cn = new SQLiteConnection(SEO.DBSqlHelp.CSDBSQLiteHelper.ConnectionSQLite());
                cn.SetPassword(SEO.Utility.CommonHelper.SQLITEPASSWORD);
                cn.Open();
                SQLiteCommand cm = new SQLiteCommand(strSql, cn);
                SQLiteDataAdapter da = new SQLiteDataAdapter(cm);
                DataSet ds = new DataSet();
                da.Fill(ds, "Information");
                ds.Tables["Information"].Columns.Add("ViewCatch", typeof(string));
                for (int i = 0; i < ds.Tables["Information"].Rows.Count; i++)
                {
                    DataRow dr = ds.Tables["Information"].Rows[i];
                }
                dgv.DataSource = ds.Tables["Information"].DefaultView;
                da.Dispose();
                cn.Close();
                //清除当前选择行行为
                dgv.CurrentCell = null;

            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("绑定Information -> " + ex.Message);
            }
        }

        /// <summary>
        /// 获取表中最大ID，并自增1
        /// </summary>
        /// <returns>返回表中最大ID，并自增1</returns>
        public int GetAutoAddNum()
        {
            int iNum = 0;
            try
            {
                SQLiteConnection cn = new SQLiteConnection(SEO.DBSqlHelp.CSDBSQLiteHelper.ConnectionSQLite());
                cn.SetPassword(SEO.Utility.CommonHelper.SQLITEPASSWORD);
                cn.Open();
                string strSql = "select se_id from Information order by se_id desc limit 1";
                SQLiteCommand cm = new SQLiteCommand(strSql, cn);
                SQLiteDataReader dr = cm.ExecuteReader();
                if (dr.Read())
                {
                    iNum = int.Parse(dr["se_id"].ToString()) + 1;
                }
                else
                {
                    iNum = 1;
                }
                dr.Dispose();
                cn.Close();
            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("绑定Information -> " + ex.Message);
            }

            return iNum;
        }

        /// <summary>
        /// 检测是否已存在数据库中，屏蔽已抓取的数据
        /// </summary>
        /// <param name="strPhone">联系电话</param>
        /// <returns>返回 True or False</returns>
        public bool CheckInformationIsExist(string strPhone)
        {
            bool bValue = false;
            try
            {
                SQLiteConnection cn = new SQLiteConnection(SEO.DBSqlHelp.CSDBSQLiteHelper.ConnectionSQLite());
                cn.SetPassword(SEO.Utility.CommonHelper.SQLITEPASSWORD);
                cn.Open();
                string strSql = "select se_id from Information where phone_result='" + strPhone + "'";
                SQLiteCommand cm = new SQLiteCommand(strSql, cn);
                SQLiteDataReader dr = cm.ExecuteReader();
                if (dr.Read())
                {
                    bValue = true;
                }
                else
                {
                    bValue = false;
                }
                dr.Dispose();
                cn.Close();
            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("检测是否已存在数据库中 -> " + ex.Message);
            }

            return bValue;
        }

    }
}
